package MyView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2017/3/15.
 */

public class SearchLayout extends ViewGroup {

    public SearchLayout(Context context) {
        super(context);
    }

    public SearchLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public SearchLayout(Context context, AttributeSet attributeSet, int a) {
        super(context, attributeSet, a);
    }

    private List<List<View>> allChildView = new ArrayList<>();

    private List<Integer> mlineheight = new ArrayList<>();

    /*
    使布局支持Margin设置
     */
    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    //指定子view的大小及位置
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        allChildView.clear();
        mlineheight.clear();
        int width = getWidth(); //获取当前viewgroup的宽度
        int lineWidth = 0;
        int lineHeight = 0;
        List<View> lineviews = new ArrayList<View>();
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childwidth = child.getMeasuredWidth();
            int childheight = child.getMeasuredHeight();

            if (childwidth + lineWidth + lp.leftMargin + lp.rightMargin > width) {
                mlineheight.add(lineHeight);
                allChildView.add(lineviews);
                lineWidth = 0;
                lineHeight = childheight + lp.topMargin + lp.bottomMargin;
                lineviews = new ArrayList<>();
            }

            lineWidth += childwidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childheight + lp.topMargin + lp.bottomMargin);
            lineviews.add(child);
        }
        mlineheight.add(lineHeight);
        allChildView.add(lineviews);
        //设置子View的位置
        int left = 0;
        int top = 0;
        //获取行数
        int lineCount = allChildView.size();
        for (int i = 0; i < lineCount; i++) {
            lineviews = allChildView.get(i);
            lineHeight = mlineheight.get(i);
            for (int j = 0; j < lineviews.size(); j++) {
                View child = lineviews.get(j);
                if (child.getVisibility() == View.GONE) {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                int cLeft = left + lp.leftMargin;
                int cTop = top + lp.topMargin;
                int cRight = cLeft + child.getMeasuredWidth();
                int cBottom = cTop + child.getMeasuredHeight();
                child.layout(cLeft, cTop, cRight, cBottom);
                left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = 0;
            top += lineHeight;
        }
    }

    /*
    负责设置子控件的测量模式和大小，根据所有的子控件设置自己的宽和高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获得父容器为它设置的测量模式和大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);


        //如果是wrap_content ，记录宽和高
        int width = 0;
        int height = 0;

        //记录行宽，width不断取最大宽度
        int linewidth = 0;
        int lineHeight = 0;
        int ccount = getChildCount();
        //遍历每个元素
        for (int i = 0; i < ccount; i++) {
            View child = getChildAt(i);
            //测量每一个child的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);

            //得到layputparams
            MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
            int childwidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childheight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

            //换行
            if (linewidth + childwidth > sizeWidth) {
                width = Math.max(width, linewidth);
                linewidth = childwidth;
                height += lineHeight;
                lineHeight = childheight;
            } else {
                //不换行
                linewidth += childwidth;
                lineHeight = Math.max(lineHeight, childheight);
            }
//            处理最后一个子view
            if (i == ccount - 1) {
                width = Math.max(width, linewidth);
                height += lineHeight;
                height += 200;
            }
        }
        //wrapcontent
        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width,
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height);
    }

}
